/**
 * 
 */
package no.hig.ekm.ircclient;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import jerklib.Session;

/**
 * @author eigil
 *
 */
public class ServerSettings extends JInternalFrame {
	private static ResourceBundle messages;
	
	public static final int CHANNEL = 0;
	public static final int PRIVATE = 1;
	public static final int CHANGENICK = 2;

	private GridBagLayout mainLayout;
	private JPanel mainPanel;
	private JLabel serv, chan, pass;
	private JComboBox servers;
	private JTextField channel, password;
	private JButton join, cancel;
	
	boolean connectedToAnyServer = false;
	private int type;
	
	
	public ServerSettings(int t) {
		super("", true);
		type = t;
		
		if(type == CHANNEL) {
			setTitle(messages.getString("channelJoinDialogTitle"));
		} else if(type == PRIVATE) {
			setTitle(messages.getString("channelJoinDialogTitlePriv"));
		} else if(type == CHANGENICK) {
			setTitle(messages.getString("channelJoinDialogTitleChange"));
		}
		
		generateGUI();
		getRootPane().setDefaultButton(join);
	}
	
	public void generateGUI() {
		mainPanel = new JPanel();
		mainPanel.setLayout(mainLayout = new GridBagLayout());
		
		generateLabels();
		generateServerList();
		generateTextFields();
		generateButtons();
		
		add(mainPanel);
		setSize(300, 165);
		setVisible(true);
	}
	
	private void generateLabels() {
		GridBagConstraints gbc = new GridBagConstraints();
		serv = new JLabel(messages.getString("channelJoinDialogLabelServer"));
		gbc.insets = new Insets(2, 2, 2, 2);
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.EAST;
		mainLayout.setConstraints(serv, gbc);
		mainPanel.add(serv);
		
		if(type == CHANNEL) {
			chan = new JLabel(messages.getString("channelJoinDialogLabelChannel"));
			gbc.gridy = 2;
			mainLayout.setConstraints(chan, gbc);
			mainPanel.add(chan);
			
			pass = new JLabel(messages.getString("channelJoinDialogLabelPassword"));
			gbc.gridy = 3;
			mainLayout.setConstraints(pass, gbc);
			mainPanel.add(pass);
		} else if (type == PRIVATE || type == CHANGENICK) {
			chan = new JLabel(messages.getString("channelJoinDialogLabelNick"));
			gbc.gridy = 2;
			mainLayout.setConstraints(chan, gbc);
			mainPanel.add(chan);
		}
	}
	private void generateServerList() {
		GridBagConstraints gbc = new GridBagConstraints();
		String[] servernames = Main.win.tree.getServerNames();
		if(servernames.length > 0) {
			servers = new JComboBox<>(servernames);
			connectedToAnyServer = true;
		} else {
			String[] s = {"<not connected to any server>"};
			servers = new JComboBox<String>(s);
		}
		servers.setEnabled(connectedToAnyServer);
		
		gbc.insets = new Insets(2, 2, 2, 2);
		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.WEST;
		mainLayout.setConstraints(servers, gbc);
		mainPanel.add(servers);
	}
	private void generateTextFields() {
		GridBagConstraints gbc = new GridBagConstraints();
		channel = new JTextField();
		gbc.insets = new Insets(2, 2, 2, 2);
		gbc.gridx = 2;
		gbc.gridy = 2;
		gbc.anchor = GridBagConstraints.EAST;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		mainLayout.setConstraints(channel, gbc);
		mainPanel.add(channel);
		
		if(type == CHANNEL) {
			password = new JTextField();
			gbc.gridy = 3;
			gbc.anchor = GridBagConstraints.EAST;
			mainLayout.setConstraints(password, gbc);
			mainPanel.add(password);
		}
	}
	private void generateButtons() {
		GridBagConstraints gbc = new GridBagConstraints();
		JPanel buttons = new JPanel();
		buttons.setLayout(new GridLayout(1,2));
		String jointxt = null;
		
		if(type == CHANNEL)
			jointxt = messages.getString("channelJoinDialogButtonJoin");
		else if (type == PRIVATE) 
			jointxt = messages.getString("channelJoinDialogButtonConverse");
		else if (type == CHANGENICK)
			jointxt = messages.getString("channelJoinDialogButtonChange");	
		
		buttons.add(join = new JButton(jointxt));
		join.setEnabled(connectedToAnyServer);
		buttons.add(cancel = new JButton(messages.getString("channelJoinDialogButtonCancel")));

		gbc.insets = new Insets(2, 2, 2, 2);
		gbc.gridx = 1;
		gbc.gridy = 4;
		gbc.gridwidth = 2;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.NONE;
		mainLayout.setConstraints(buttons, gbc);
		mainPanel.add(buttons);
		
		join.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(ServerSettings.this.type == ServerSettings.CHANNEL) {
					try {
						if(password.getText().isEmpty())
							Main.cm.joinChannel((String) servers.getSelectedItem(), channel.getText());
						else
							Main.cm.joinChannel((String) servers.getSelectedItem(), channel.getText(), password.getText());
					} catch (IRCClientException e) {
					}
				} else if (ServerSettings.this.type == ServerSettings.PRIVATE) {
					ServerEventListener sel = null;
					Session s = Main.cm.getSession((String)servers.getSelectedItem());
					
					if(s == null)
						return;
					
					Object[] ircel = s.getIRCEventListeners().toArray();
					for(int i = 0; i < ircel.length; i++) {
						if(ircel[i] instanceof ServerEventListener)
							 sel = (ServerEventListener) ircel[i];
					}
					
					if(sel == null)
						return;
					
					sel.privMessageWindow(s, channel.getText());
				} else if (ServerSettings.this.type == ServerSettings.CHANGENICK) {
					Session s = Main.cm.getSession((String)servers.getSelectedItem());
					s.changeNick(channel.getText());
				}
				
				Main.win.md.remove(ServerSettings.this);
				Main.win.md.repaint();
				ServerSettings.this.dispose();
			}
		});
		
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Main.win.md.remove(ServerSettings.this);
				Main.win.md.repaint();
				ServerSettings.this.dispose();
			}
		});
	}
	
	public static void setMessages(ResourceBundle m) {
		messages = m;
	}
}
